using MongoDB.Bson;
using MongoDB.Driver;
using Sustainalytics.BusinessResearchProductStates.Entities.ResearchState;
using Sustainalytics.Entities.ScreeningTool;
using System.Threading.Tasks;

namespace Sustainalytics.ScreeningTool.Synchronization
{
    public class ScreeningDataCreator
    {
        private readonly IMongoCollection<ScreeningData> _screeningDataCollection;
        private readonly StandardFieldsCreator _standardFieldsCreator;
        private readonly ExtraElementsCreator _extraElementsCreator;

        public ScreeningDataCreator(
            IMongoCollection<ScreeningData> screeningDataCollection,
            StandardFieldsCreator standardFieldsCreator,
            ExtraElementsCreator extraElementsCreator)
        {
            _screeningDataCollection = screeningDataCollection;
            _standardFieldsCreator = standardFieldsCreator;
            _extraElementsCreator = extraElementsCreator;
        }

        public async Task CreateScreeningDataAsync(ClientFacingProduct clientFacingProduct)
        {
            await ClearScreeningDataAsync().ConfigureAwait(false);
            await _standardFieldsCreator.AddStandardFieldsAsync(clientFacingProduct).ConfigureAwait(false);
            await _standardFieldsCreator.AddSecurityListIdsAsync(clientFacingProduct).ConfigureAwait(false);
            await _extraElementsCreator.AddExtraElementsForResearchEntitiesAsync().ConfigureAwait(false);
            await _extraElementsCreator.AddExtraElementsForCoverageEntitiesAsync().ConfigureAwait(false);
        }

        private async Task ClearScreeningDataAsync()
        {
            await _screeningDataCollection.DeleteManyAsync(new BsonDocument()).ConfigureAwait(false);
        }
    }
}
